package com.rtst.widget.listview;

import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MyAdapter extends BaseAdapter {
	AbstractBuilder mBuilder;
	private int mLimitNumber = -1;
	public MyAdapter(AbstractBuilder builder) {
		mBuilder = builder;
		mLimitNumber = -1;
	}

	@Override
	public int getCount() {
		return mLimitNumber < 0? mBuilder.getData().size() : Math.min(mLimitNumber, mBuilder.getData().size());
	}

	@Override
	public Object getItem(int position) {
		return mBuilder.getData().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private static class ViewHolder {
		List<View> views;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		synchronized (mBuilder.getData()) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mBuilder.buildViewHolder();
				holder.views = mBuilder.buildViewHolderContent(convertView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			mBuilder.buildViewHolderContentData(position, holder.views, mBuilder.getData());
			return convertView;
		}
	}
	
	public void viewLimit(int number) {
		if(number != mLimitNumber)
		{
			mLimitNumber = number;
			notifyDataSetChanged();
		}
	}
	
	public boolean showMoreAble() {
		return (mBuilder.getData().size() > mLimitNumber) && (mLimitNumber > 0);
	}
}
